/*
 *  Copyright (c) 2013.
 *  * 游戏服务器核心代码编写人陈磊拥有使用权
 *  * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 *  * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.proto.itemserverprotoVo.*;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.RoleItemService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleBook;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleEqu;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TSprite;
import com.dc.gameserver.serverCore.model.gamemodel.BookModel;
import com.dc.gameserver.serverCore.model.gamemodel.EquipmentModel;
import com.dc.gameserver.serverCore.model.gamemodel.ItemCell;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.dc.gameserver.serverCore.service.effective.IEffective;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 石头哥哥 on 13-12-16.
 * 穿上装备（秘籍）
 * 角色属性改变
 *
 *
 *
 装备附带属性
 攻击法宝：攻击类属性
 仙衣：防御类属性
 配饰：法力值类属性
 挂件：法术盾类属性
 神识法宝：多张神牌可触发的技能

 注意 角色穿装备 和脱装备 对应的属性会变更

 灵泉 灵脉 蒲团更换
 *
 */
@Service
@SuppressWarnings("unchecked")
public class putOnController extends AbstractController {

    @Resource
    private RoleItemService roleItemService;

    @Resource
    private ModelCacheService modelCacheService;
    @Resource
    private PlayerService playerService;
   @Override
    public void PostConstruct() {
       int ID= putOnItmeRequest.msgID.ID_VALUE;
       GAME_CONTROLLERS[ID]=this;
       IController.MESSAGE_LITE[ID]= putOnItmeRequest.getDefaultInstance();
    }

    /**
     *
     * @param messageLite     数据载体
     * @param player active object
     * @throws Exception
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        int roleID=player.getID();
        putOnItmeRequest request= (putOnItmeRequest) messageLite;
        int itemID=request.getItemID();
        int itemType=request.getItemType();
        int itemLocation=request.getItemLocation();
        request=null;
        ItemCell cell=player.getCellByLocation(itemLocation);   //点击将会被装备的位置
        int type=cell.getType(); //类型 部位  针对功法境界限制 后期要处理

        ItemCell otherCell=player.checkItemCellByItemID(itemID,itemType);//已经装备的位置,如果存在


        if (otherCell!=null&&otherCell.getItemLocation()!=itemLocation){
            player.resetItemCell(otherCell);
        }

        //判定cell对象上是存在物品
        boolean haveGoods=false;
        if (cell.getItemID()!= 0x0){   //来自配置文件编号
            haveGoods=true;   //存在对象 替换
        }

        //装备
        if (itemType== 0x1){
            attrVoResponse.Builder _builder=attrVoResponse.newBuilder();
            TRoleEqu putOneEqu=null;
            List<TRoleEqu>equList= (List<TRoleEqu>) modelCacheService.findListByPrefixID(ModelCacheService.EQU_PREFIX(roleID));
            for (TRoleEqu equ:equList){
                if (equ.getEquid()==itemID){
                    //请求类型有错误
                    if (equ.getEqutype()!=type){
                        return;
                    }
                    putOneEqu=equ;
                    equList.remove(equ);
                    break;
                }
            }

            if (putOneEqu!=null){
                //notify    and change attr
                putOnItmeResponse.Builder builder=putOnItmeResponse.newBuilder();
                putOneEqu.setEqulocation((byte)itemLocation);
                //update db
                roleItemService.putOn(putOneEqu);
                equList.add(putOneEqu);

                if (haveGoods){
                   //replace goods
                    int replaceItemID=cell.getItemID();
                    TRoleEqu replaceEqu=null;
                    for (TRoleEqu equ:equList){
                         if (equ.getEquid()==replaceItemID){
                             replaceEqu=equ;
                             equList.remove(equ);
                             break;
                         }
                    }
                    if (replaceEqu!=null){
                        replaceEqu.setEqulocation((byte) 0xffffffff);
                    }
                    //更新replace
                    roleItemService.putOn(replaceEqu);//update db
                    equList.add(replaceEqu);  //add to cache


                    //take off equ change attr
                    EquipmentModel model=ScriptFactory.equipmentModelFastMap.get(replaceEqu.getEqutypeid());
                    int goodsID=replaceEqu.getEquid();
                    int flag= 0x1;//1.number 2.percent  ,default is 1
                    int effectType=model.getEffectType1();
                    if (model.getEffectNum1()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,false,null);

                    flag= 0x1;//1.number 2.percent  ,default is 1
                    effectType=model.getEffectType2();
                    if (model.getEffectNum2()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,false,null);

                    flag= 0x1;//1.number 2.percent  ,default is 1
                    effectType=model.getEffectType3();
                    if (model.getEffectNum3()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,false,null);


                    //put on other equ  change attr
                    model=ScriptFactory.equipmentModelFastMap.get(putOneEqu.getEqutypeid());
                    goodsID=putOneEqu.getEquid();
                    flag= 0x1;//1.number 2.percent  ,default is 1
                    effectType=model.getEffectType1();
                    if (model.getEffectNum1()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    flag= 0x1;
                    effectType=model.getEffectType2();
                    if (model.getEffectNum2()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    flag= 0x1;
                    effectType=model.getEffectType3();
                    if (model.getEffectNum3()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    //change location obj
                    cell.setItemID(putOneEqu.getEquid());
                    cell.setItemTypeID(putOneEqu.getEqutypeid());
                    cell.setItemType(putOneEqu.getItemtype());
                    builder.setReplace(0x2); //存在对象 被替换
                    builder.setRItemID(replaceItemID);
                    builder.setRItemType(replaceEqu.getItemtype());
                    builder.setRItemLocation(0xffffffff);
                    builder.setItemID(putOneEqu.getEquid());
                    builder.setItemType(putOneEqu.getItemtype());
                    builder.setItemLocation(putOneEqu.getEqulocation());


                }else {

                    //穿装备
                    //change location obj
                    cell.setItemID(putOneEqu.getEquid());
                    cell.setItemTypeID(putOneEqu.getEqutypeid());
                    cell.setItemType(putOneEqu.getItemtype());
                    //put on other equ  change attr
                    EquipmentModel model=ScriptFactory.equipmentModelFastMap.get(putOneEqu.getEqutypeid());
                    int goodsID=putOneEqu.getEquid();
                    int effectType=model.getEffectType1();
                    int flag= 0x1;//1.number 2.percent  ,default is 1
                    if (model.getEffectNum1()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);


                    flag= 0x1;
                    effectType=model.getEffectType2();
                    if (model.getEffectNum2()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);


                    flag= 0x1;
                    effectType=model.getEffectType3();
                    if (model.getEffectNum3()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    builder.setReplace(0x1);
                    builder.setItemID(putOneEqu.getEquid());
                    builder.setItemType(putOneEqu.getItemtype());
                    builder.setItemLocation(putOneEqu.getEqulocation());

                }
                //last update cache
                modelCacheService.addListByPrefixID(ModelCacheService.EQU_PREFIX(roleID), equList);
                // notify attr and goods status
                player.DirectSend(wrappedBufferShort(putOnItmeResponse.msgID.ID_VALUE,builder.build()));
                player.DirectSend(wrappedBufferShort(attrVoResponse.msgID.ID_VALUE,_builder.build()));
            }
            return;
        }


        //秘籍
        if (itemType== 0x2){
            attrVoResponse.Builder _builder=attrVoResponse.newBuilder();
            TRoleBook putOnbook=null;
            List<TRoleBook>bookList= (List<TRoleBook>) modelCacheService.findListByPrefixID(ModelCacheService.BOOK_PREFIX(roleID));
            for (TRoleBook tRoleBook:bookList){
                if (tRoleBook.getBookid()==itemID){
                    //请求类型有错误
                    if (tRoleBook.getBooktype()!=type){
                        return;
                    }
                    putOnbook=tRoleBook;
                    bookList.remove(tRoleBook);
                    break;
                }
            }

            if (putOnbook!=null){
                putOnItmeResponse.Builder builder=putOnItmeResponse.newBuilder();

                putOnbook.setBooklocation((byte)itemLocation);//change put on book
                roleItemService.putOn(putOnbook);
                bookList.add(putOnbook);

                if (haveGoods){   //替换秘籍类
                    //替换
                    int replaceItemID=cell.getItemID();
                        TRoleBook replaceBook=null;
                        for (TRoleBook roleBook:bookList){
                            if (roleBook.getBookid()==replaceItemID){
                                replaceBook=roleBook;
                                bookList.remove(roleBook);
                                break;
                            }
                    }
                    if (replaceBook!=null){
                        replaceBook.setBooklocation((byte) 0xffffffff);
                    }

                    roleItemService.putOn(replaceBook);//change replace book
                    bookList.add(replaceBook);


                    //change location obj
                    cell.setItemID(putOnbook.getBookid());
                    cell.setItemTypeID(putOnbook.getBooktypeid());
                    cell.setItemType(putOnbook.getItemtype());


                    //take off equ change attr
                    BookModel model=ScriptFactory.bookModelFastMap.get(replaceBook.getBooktypeid());
                    int goodsID=replaceBook.getBookid();
                    int flag= 0x1;//1.number 2.percent  ,default is 1
                    int effectType=model.getEffectType1();
                    if (model.getEffectNum1()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,false,null);

                    flag= 0x1;//1.number 2.percent  ,default is 1
                    effectType=model.getEffectType2();
                    if (model.getEffectNum2()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,false,null);

                    flag= 0x1;//1.number 2.percent  ,default is 1
                    effectType=model.getEffectType3();
                    if (model.getEffectNum3()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,false,null);


                    //put on other equ  change attr
                    model=ScriptFactory.bookModelFastMap.get(putOnbook.getBooktypeid());
                    goodsID=putOnbook.getBookid();
                    flag= 0x1;//1.number 2.percent  ,default is 1
                    effectType=model.getEffectType1();
                    if (model.getEffectNum1()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    flag= 0x1;
                    effectType=model.getEffectType2();
                    if (model.getEffectNum2()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    flag= 0x1;
                    effectType=model.getEffectType3();
                    if (model.getEffectNum3()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);


                    builder.setReplace(0x2);
                    builder.setRItemID(replaceItemID);
                    builder.setRItemType(replaceBook.getItemtype());
                    builder.setRItemLocation(0xffffffff);

                    builder.setItemID(putOnbook.getBookid());
                    builder.setItemType(putOnbook.getItemtype());
                    builder.setItemLocation(putOnbook.getBooklocation());

                }else {
                    //穿戴

                    //change location obj
                    cell.setItemID(putOnbook.getBookid());
                    cell.setItemTypeID(putOnbook.getBooktypeid());
                    cell.setItemType(putOnbook.getItemtype());

                    //put on other equ  change attr
                    BookModel model=ScriptFactory.bookModelFastMap.get(putOnbook.getBooktypeid());
                    int effectType=model.getEffectType1();
                    int goodsID=putOnbook.getBookid(); //编号
                    int flag= 0x1;//1.number 2.percent  ,default is 1
                    if (model.getEffectNum1()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    flag= 0x1;
                    effectType=model.getEffectType2();
                    if (model.getEffectNum2()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);

                    flag= 0x1;
                    effectType=model.getEffectType3();
                    if (model.getEffectNum3()==0)flag= 0x2;
                    if (effectType!=0)IEffective.EFFECTIVE_ID[effectType].effect(player,model,goodsID,flag,true,_builder);


                    builder.setReplace(0x1);
                    builder.setItemID(putOnbook.getBookid());
                    builder.setItemType(putOnbook.getItemtype());
                    builder.setItemLocation(putOnbook.getBooklocation());

                }
                modelCacheService.addListByPrefixID(ModelCacheService.BOOK_PREFIX(roleID),bookList);
                player.DirectSend(wrappedBufferShort(putOnItmeResponse.msgID.ID_VALUE,builder.build()));
                player.DirectSend(wrappedBufferShort(attrVoResponse.msgID.ID_VALUE,_builder.build()));
            }
            return;
        }




        //灵脉 灵泉  蒲团   穿戴  更换
        if (itemType== 0x4){
            TSprite putOnSprite=null;
            spriteResponse.Builder spriteBuilder=spriteResponse.newBuilder();
            List<TSprite>spriteList= (List<TSprite>) modelCacheService.findListByPrefixID(ModelCacheService.SPRITE_PREFIX(roleID));
            for (TSprite tSprite:spriteList){
                if (tSprite.getSpiriteid()==itemID){
                    //请求类型有错误
                    if (tSprite.getSpiritetype()!=type){
                        return;
                    }
                    putOnSprite=tSprite;
                    spriteList.remove(tSprite);
                    break;
                }
            }

            if (putOnSprite!=null){
                putOnItmeResponse.Builder builder=putOnItmeResponse.newBuilder();
                putOnSprite.setSpritelocation((byte) itemLocation);//change put on book

                //update and  to cache
                roleItemService.putOn(putOnSprite);
                spriteList.add(putOnSprite);

                TRole tRole=null;
                if (haveGoods){   //替换精舍类
                    //替换
                    int replaceItemID=cell.getItemID();
                    TSprite replaceSprite=null;
                    for (TSprite sprite:spriteList){
                        if (sprite.getSpiriteid()==replaceItemID){
                            replaceSprite=sprite;
                            spriteList.remove(sprite);
                            break;
                        }
                    }
                    if (replaceSprite!=null){
                        replaceSprite.setSpritelocation((byte) 0xffffffff);
                    }

                    playerService.updateSprite(replaceSprite);
                    spriteList.add(replaceSprite);

                    String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
                    tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);

                    /**
                     * 计算被更换的对象产生的灵气
                     */
                    if (checkSprite(player,tRole)){
                        spriteCompute(player,tRole,System.currentTimeMillis(),modelCacheService,playerService,spriteList,false);
                        playerService.updateRole(tRole);
                        modelCacheService.addObjectPrefixID(role_prefix,tRole);
                    }


                    //change location obj
                    cell.setItemID(putOnSprite.getSpiriteid());
                    cell.setItemTypeID(putOnSprite.getSpiritetypeid());
                    cell.setItemType(putOnSprite.getItemtype());



                    //构建发送数据
                    builder.setReplace(0x2);
                    builder.setRItemID(replaceItemID);
                    builder.setRItemType(replaceSprite.getItemtype());
                    builder.setRItemLocation(0xffffffff);

                    builder.setItemID(putOnSprite.getSpiriteid());
                    builder.setItemType(putOnSprite.getItemtype());
                    builder.setItemLocation(putOnSprite.getSpritelocation());

                }else {

                    //穿戴
                    //change location obj
                    cell.setItemID(putOnSprite.getSpiriteid());
                    cell.setItemTypeID(putOnSprite.getSpiritetypeid());
                    cell.setItemType(putOnSprite.getItemtype());

                    String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
                    tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);


                    /**
                     * 计算被更换的对象产生的灵气
                     */
                    if (checkSprite(player,tRole)){
                        spriteCompute(player,tRole,System.currentTimeMillis(),modelCacheService,playerService,spriteList,false);
                        playerService.updateRole(tRole);
                        modelCacheService.addObjectPrefixID(role_prefix,tRole);
                    }

                    builder.setReplace(0x1); //1. 对应的位置不存在物品穿戴, 2. 对应的位置存在物品(替换)
                    builder.setItemID(putOnSprite.getSpiriteid());
                    builder.setItemType(putOnSprite.getItemtype());
                    builder.setItemLocation(putOnSprite.getSpritelocation());
                }



                /**构建数据**/
                spriteBuilder.setSpriteStore(tRole.getStorelingqi());
                FastTable<ItemCell> spriteCellList=player.getSpriteItem();
                for (ItemCell _cell:spriteCellList){
                    int _itemID=_cell.getItemID();
                    for (TSprite sprite:spriteList){
                        if (sprite.getSpiriteid()==_itemID){
                            spriteinformation.Builder __builder=spriteinformation.newBuilder();
                            __builder.setSpriteLocation(_cell.getItemLocation());
                            __builder.setSpriteLast(sprite.getSpiritecurrent());  //灵泉当前剩余值
                            spriteBuilder.addInforList(__builder.build());
                            break;
                        }
                    }
                }
                spriteCellList.clear();
                spriteCellList=null;
                player.DirectSend(wrappedBufferShort(spriteResponse.msgID.ID_VALUE,spriteBuilder.build()));


                player.DirectSend(wrappedBufferShort(putOnItmeResponse.msgID.ID_VALUE,builder.build()));
            }


        }
    }




}
